dnl	This file is an input file used by the GNU "autoconf" program to
dnl	generate the file "configure", which is run during installation
dnl	to configure the system for the local environment.

AC_INIT(src/itcl_core.c)

AC_PROG_INSTALL
AC_PROG_RANLIB
AC_PROG_CPP

CC=${CC-cc}
AC_SUBST(CC)

#--------------------------------------------------------------------
#	Check for various typedefs and provide substitutes if
#	they don't exist.
#--------------------------------------------------------------------

AC_MODE_T
AC_PID_T
AC_SIZE_T
AC_UID_T

#--------------------------------------------------------------------
#	Locate the X11 header files and the X11 library archive.  Try
#	the ac_find_x macro first, but if it doesn't find the X stuff
#	(e.g. because there's no xmkmf program) then check through
#	a list of possible directories.
#--------------------------------------------------------------------

AC_PROGRAM_CHECK(tk_ok, xmkmf, 1, 0)
if test $tk_ok = 1; then
    AC_FIND_X
fi
if test "$x_includes" = /usr/include; then
    XINCLUDES="# no special path needed"
elif test "$x_includes" != ""; then
    XINCLUDES=" -I$x_includes"
else
    echo checking for X11 header files
    XINCLUDES="# no special path needed"
    AC_TEST_CPP([#include <X11/Intrinsic.h>], , XINCLUDES="nope")
    if test "$XINCLUDES" = nope; then
        dirs=${XINCLUDE_DIR-"/usr/unsupported/include /usr/local/include /usr/X386/include /usr/include/X11R4 /usr/X11R5/include /usr/include/X11R5 /usr/openwin/include /usr/X11/include"}
        for i in $dirs ; do
	    if test -r $i/X11/Intrinsic.h; then
	        XINCLUDES=" -I$i"
	    fi
        done
    fi
fi
if test "$XINCLUDES" = nope; then
  echo "Warning:  couldn't find any X11 include files."
  XINCLUDES="# no include files found"
fi
AC_SUBST(XINCLUDES)

if test "$x_libraries" = /usr/lib; then
    XLIBSW=-lX11
elif test "$x_libraries" != ""; then
    XLIBSW="-L$x_libraries -lX11"
else
    echo "checking for X11 library archive"
    AC_HAVE_LIBRARY(X11, XLIBSW="-lX11", XLIBSW=nope)
    if test "$XLIBSW" = nope; then
	dirs=${XLIBRARY_DIR-"/usr/unsupported/lib /usr/local/lib /usr/X386/lib /usr/lib/X11R4 /usr/X11R5/lib /usr/lib/X11R5 /usr/openwin/lib /usr/X11/lib"}
	for i in $dirs ; do
	    if test -r $i/libX11.a; then
		XLIBSW="-L$i -lX11"
	    fi
	done
    fi
fi
if test "$XLIBSW" = nope ; then
    AC_HAVE_LIBRARY(Xwindow, XLIBSW=-lXwindow)
fi
if test "$XLIBSW" = nope ; then
    echo "Warning:  couldn't find the X11 library archive.  Using -lX11."
    XLIBSW=-lX11
fi
AC_SUBST(XLIBSW)

#--------------------------------------------------------------------
#	Check for the existence of various libraries.  The order here
#	is important, so that then end up in the right order in the
#	command line generated by Make.
#--------------------------------------------------------------------
AC_HAVE_LIBRARY(m, [LIBS="$LIBS -lm"])

#--------------------------------------------------------------------
# Make some guesses where the Tcl/Tk include files and libraries are.
#--------------------------------------------------------------------
PWD=`pwd`

AC_HEADER_CHECK(tcl.h,, [
if test -f $PWD/../../../tcl/tcl-7.0/tclInt.h ; then
  TCL_INCDIR=$PWD/../../../tcl/tcl-7.0
elif test -f $PWD/../tcl7.0/tclInt.h ; then
  TCL_INCDIR=$PWD/../tcl7.0
elif test -f $PWD/../tcl7.0b3/tclInt.h ; then
  TCL_INCDIR=$PWD/../tcl7.0b3
elif test -f $x_includes/tk/tclInt.h ; then
  TCL_INCDIR=$x_includes/tk
elif test -f /usr/local/include/tclInt.h ; then
  TCL_INCDIR=/usr/local/include
else
  echo "What directory contains Tcl source code (absolute path) ?"
  read TCL_INCDIR
fi
echo "setting TCL_INCDIR as $TCL_INCDIR"
AC_SUBST(TCL_INCDIR)
])

TCL_LIBDIR=unknown
if test -f $PWD/../../../tcl/tcl7.0/libtcl.a ; then
  TCL_LIBDIR=$PWD/../../../tcl/tcl7.0
elif test -f $PWD/../tcl7.0/libtcl.a ; then
  TCL_LIBDIR=$PWD/../tcl7.0
elif test -f $PWD/../tcl7.0b3/libtcl.a ; then
  TCL_LIBDIR=$PWD/../tcl7.0b3
elif test -f $x_libraries/libtcl.a ; then
  TCL_LIBDIR=$x_libraries
elif test -f /usr/local/lib/libtcl.a ; then
  TCL_LIBDIR=/usr/local/lib
else
  echo "What directory contains libtcl.a ?"
  read TCL_LIBDIR
fi
echo "setting TCL_LIBDIR as $TCL_LIBDIR"
AC_SUBST(TCL_LIBDIR) 

AC_HEADER_CHECK(tk.h,, [
if test -f $PWD/../../../tk/tk-3.3/tk.h ; then
  TK_INCDIR=$PWD/../../../tk/tk-3.3
elif test -f $PWD/../tk3.3/tk.h ; then
  TK_INCDIR=$PWD/../tk3.3
elif test -f $PWD/../tk3.3b3/tk.h ; then
  TK_INCDIR=$PWD/../tk3.3b3
elif test -f $x_includes/tk/tk.h ; then
  TK_INCDIR=$x_includes/tk
elif test -f /usr/local/include/tk.h ; then
  TK_INCDIR=/usr/local/include
else
  echo "What directory contains tk.h (absolute path) ?"
  read TK_INCDIR
fi
echo "setting TK_INCDIR as $TK_INCDIR"
AC_SUBST(TK_INCDIR)
])

TK_LIBDIR=unknown
if test -f $PWD/../../../tk/tk3.3/libtk.a ; then
  TK_LIBDIR=$PWD/../../../tk/tk3.3
elif test -f $PWD/../tk3.3/libtk.a ; then
  TK_LIBDIR=$PWD/../tk3.3
elif test -f $PWD/../tk3.3b3/libtk.a ; then
  TK_LIBDIR=$PWD/../tk3.3b3
elif test -f $x_libraries/libtk.a ; then
  TK_LIBDIR=$x_libraries
elif test -f /usr/local/lib/libtk.a ; then
  TK_LIBDIR=/usr/local/lib
else
  echo "What directory contains libtk.a ?"
  read TK_LIBDIR
fi
echo "setting TK_LIBDIR as $TK_LIBDIR"
AC_SUBST(TK_LIBDIR) 

#--------------------------------------------------------------------
# Check to see if compiler will handle "-pic" for shared libraries
#--------------------------------------------------------------------
CFLAGS_save=$CFLAGS
CFLAGS="$CFLAGS -pic"
AC_COMPILE_CHECK("shared library support", , [
	int i; double d;
	for (i=0; i<10; i++) d=2*i;
], can_do_shared_libs=1, can_do_shared_libs=0)
CFLAGS=$CFLAGS_save

if eval "ld -assert pure-text >/dev/null 2>&1"; then
	can_do_shared_libs=$can_do_shared_libs
else
	can_do_shared_libs=0
fi

if test $can_do_shared_libs = 1; then
	SHARED='libitcl.so.$(VERSION)'
else
	SHARED=''
	echo "== DON'T KNOW HOW TO DO SHARED LIBRARIES ON THIS MACHINE =="
fi
AC_SUBST(SHARED) 

AC_OUTPUT(Makefile src/Makefile man/Makefile)
